package com.edu.component;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.github.pagehelper.Page;
import lombok.Data;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

//https://www.cnblogs.com/likui-bookHouse/p/11207127.html
@Data
public class PageBean <T> {
    private int total = 0;
    private List<T> rows = new ArrayList<>();
    /*
        1.编写构造器
           PageBean()
           PageBean(Page<T> page)
     */
    public PageBean(){}
    public PageBean(Page<T> page){
        this.total =(int)page.getTotal();
        this.rows = page.getResult();
    }

    public String toJson() {
        JSONObject jsObj = new JSONObject();
        jsObj.put("code",0);
        jsObj.put("total",total);
        jsObj.put("rows",rows);
        return jsObj.toString();
    }
}
